package team.cw.land.test.activiti.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import org.activiti.engine.task.Task;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import team.cw.land.test.activiti.dto.ActivitiDTO;
import team.cw.land.test.activiti.dto.ActivitiHistoryDTO;
import team.cw.land.test.activiti.service.IActivitiService;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/demo")
public class DemoController {

    private static String processName = "案件流程";

    @Autowired
    private IActivitiService activitiService;

    @GetMapping(value="/start")
    @ResponseBody
    public String start(){
        Map<String,Object> map = new HashMap<>();
        map.put("user","国土张三");
        map.put("user1","国土张三");
        map.put("user2","国土张三");
        map.put("user3","国土大队长");
        map.put("user4","国土副支队长");
        map.put("user5","国土支队长");
        map.put("user6","监察李四");
        map.put("user7","监察负责人");
        map.put("user8","分管领导");
        map.put("user9","分管王五");
        String processId = activitiService.startProcess(processName, map);
        return processId;
    }

    @GetMapping("/taskList")
    @ResponseBody
    public JSONArray getTaskList(HttpServletRequest request){
        HttpSession session = request.getSession();
        List<Task> taskList = activitiService.myTaskList(session.getAttribute("userName").toString());
        JSONArray jsonArray = new JSONArray();
        taskList.forEach(task -> {
            JSONObject object = new JSONObject();
            object.put("Id",task.getId());
            object.put("Name",task.getName());
            object.put("processInstanceId",task.getProcessInstanceId());
            jsonArray.add(object);
        });
        return jsonArray;
    }

    @ResponseBody
    @GetMapping(value="/showImg")
    public void showImg(String instanceId, HttpServletResponse response) throws IOException {
        byte[] processImg = activitiService.getProcessImg(instanceId);
        response.getOutputStream().write(processImg);
        response.getOutputStream().flush();
    }

    @ResponseBody
    @PostMapping(value = "/complete")
    public String complete(HttpServletRequest request,String taskId,String processInstanceId,String remark,String comment,String type){
        HttpSession session = request.getSession();
        Map<String, Object> vars = new HashMap<>();
        vars.put("remark", remark);
        List comments = new ArrayList();
        comments.add(comment);
        if(StringUtils.isNotBlank(type)){
            vars.put("type", type);
            comments.add(comment);
        }
        activitiService.myCompleteProcess(session.getAttribute("userName").toString(),processInstanceId,taskId,comments,vars);
        return "success";
    }

    @ResponseBody
    @GetMapping("/activitiList")
    public List<ActivitiDTO> underComplete(HttpServletRequest request){
        return activitiService.getActivitiHistoryList(null);
    }

    @ResponseBody
    @GetMapping("/activitiInfo")
    public List<ActivitiHistoryDTO> underComplete(HttpServletRequest request, String processId){
        return activitiService.getActivitiHistoryInfo(processId);
    }

    @ResponseBody
    @GetMapping("/delete")
    public boolean delete(String processId){
        return activitiService.deleteProcessId(processId);
    }
}
